//
//  ResetPasswordViewController.m
//  lhpc_manager
//
//  Created by 李英杰 on 2017/9/6.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import "InputPassWordView.h"
#import "LHNetworking.h"

@interface ResetPasswordViewController ()

@property (nonatomic ,strong) InputPassWordView * setPassWordView;

@property (nonatomic ,strong) InputPassWordView * confirmPassWordView;

@property (nonatomic, strong) UIButton * confirmButton;

@end

@implementation ResetPasswordViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"设置密码";
    
    [self setMainView];
    
}


-(void)comfirmButtonAction{
    
    if (self.setPassWordView.passwordTextField.text.length == 0 || self.confirmPassWordView.passwordTextField.text.length == 0) {
        [HLProgressHUD showHudTipStr:@"输入密码不能为空"];
        return;
    }
    
    if (self.setPassWordView.passwordTextField.text.length != 6 || self.confirmPassWordView.passwordTextField.text.length != 6) {
        [HLProgressHUD showHudTipStr:@"请确认输入密码为6位"];
        return;
    }
    
    
    if (![self.setPassWordView.passwordTextField.text isEqualToString:self.confirmPassWordView.passwordTextField.text]) {
        [HLProgressHUD showHudTipStr:@"两次输入密码不一致"];
        return;
    }
    
    NSDictionary * parameters = @{@"password1":[self.setPassWordView.passwordTextField.text hl_md5],
                                  @"password2":[self.confirmPassWordView.passwordTextField.text hl_md5]};
    
    LHPC_WEAK_SELF;
    [[LHNetworking shared] POST:LHSetRequestAPI(LHPasswordSet) parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
        LHPC_STRONG_SELF;
        
        if (responseObject) {
            [HLNoteCenter postNotificationName:LHReloadUnCompleteHome object:nil];
            [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleSuccess status:@"提现密码设置成功!"];
            [strongSelf.navigationController popToRootViewControllerAnimated:YES];
        }
        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [HLProgressHUD showHudTipStr:LHNetWorkErrorMessage];
    }];
    
    
    NSLog(@"确认设置");
}


-(void)setMainView{
    self.view.backgroundColor = BACKCOLOR;
    
    [self.view addSubview:self.setPassWordView];
    [self.view addSubview:self.confirmPassWordView];
    
    [self.view addSubview:self.confirmButton];
    
    [self updateViewConstraints];
}



-(void)updateViewConstraints{
    [super updateViewConstraints];
    
    [self.setPassWordView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(10);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@55);
        
    }];
    
    
    [self.confirmPassWordView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.setPassWordView.mas_bottom).offset(10);
        make.left.right.equalTo(self.view);
        make.height.equalTo(self.setPassWordView);
    }];
    
    [self.confirmButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.confirmPassWordView.mas_bottom).offset(50);
        make.left.equalTo(self.view).offset(30);
        make.right.equalTo(self.view).offset(-30);
        make.height.equalTo(@40);
        
        
    }];
    
}


-(InputPassWordView *)setPassWordView{
    if (!_setPassWordView) {
        _setPassWordView = [[InputPassWordView alloc]init];
        _setPassWordView.titleStr = @"输入密码";
        _setPassWordView.placeholderStr = @"请输入6位数密码";
    }
    return _setPassWordView;
}

-(InputPassWordView *)confirmPassWordView{
    if (!_confirmPassWordView) {
        _confirmPassWordView = [[InputPassWordView alloc]init];
        _confirmPassWordView.titleStr = @"确认密码";
        _confirmPassWordView.placeholderStr = @"请确认6位数密码";
    }
    return _confirmPassWordView;
}

-(UIButton *)confirmButton{
    if (!_confirmButton) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmButton.layer.cornerRadius = 3;
        [_confirmButton setBackgroundColor:ORANGCOLOR];
        [_confirmButton setTitle:@"确认" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_confirmButton addTarget:self action:@selector(comfirmButtonAction) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton.userInteractionEnabled = YES;
        
    }
    return _confirmButton;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

@end
